package com.ysd.service.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.service.dao.UserDao;
import com.ysd.service.dto.UserDto;
import com.ysd.service.entity.Clock;
import com.ysd.service.entity.User;
import com.ysd.service.util.IBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends ServiceImpl<UserDao,User> {

    @Autowired
    private UserDao userDao;

    /**
     * insert   添加用户
     * @param param
     */
    public void addUser(User param){
        userDao.insert(param);
    }

    /**
     * update   编辑用户
     * @param param
     */
    public void editorUser(User param){
        LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(User::getOpenId, param.getOpenId());
        updateWrapper.set(User::getNickName, param.getNickName());
        updateWrapper.set(User::getAvatar, param.getAvatar());
        userDao.update(param, updateWrapper);
    }

    /**
     * delete   删除用户
     * @param openId
     */
    public void delUser(String openId){
        LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(User::getOpenId, openId);
        userDao.delete(updateWrapper);
    }

    /**
     * 查询该openid是否存在
     * @param openid
     * @return
     */
    public Boolean isExistOpenId(String openid){
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getOpenId, openid);
        queryWrapper.eq(User::getDelFlag, 0);
        List<User> userList = userDao.selectList(queryWrapper);
        return userList.size() > 0;
    }

    /**
     * 根据openid查询用户详情
     * @param openid
     * @return
     */
    public User getUserDetailByOpenid(String openid){
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getOpenId, openid);
        queryWrapper.eq(User::getDelFlag, 0);
        return userDao.selectOne(queryWrapper);
    }

    /**
     * 修改用户头像
     * @param param
     */
    public void editUserAvatar(UserDto param){
        LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(User::getOpenId, param.getOpenid());
        updateWrapper.eq(User::getDelFlag, 0);
        updateWrapper.set(User::getAvatar, param.getAvatar());
        userDao.update(IBeanUtils.copyProperties(param, User.class), updateWrapper);
    }

    /**
     * 修改用户头像
     * @param param
     */
    public void editUserNickName(UserDto param){
        LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(User::getOpenId, param.getOpenid());
        updateWrapper.eq(User::getDelFlag, 0);
        updateWrapper.set(User::getNickName, param.getNickName());
        userDao.update(IBeanUtils.copyProperties(param, User.class), updateWrapper);
    }


}
